<template>
	<uni-loading v-if="loading"></uni-loading>
	<view class="body" v-else>
		<!--  顶部  -->
		<top-tab :options="stockList" :locationName="locationName" @stockChange="stockChange"
			@handleBarnChoose="handleBarnChoose"></top-tab>
			
		<view class="top-container no-top" style="height: 44px;">
			<view class="top-container-inner">
				<uni-list :border="false">
					<uni-list-item :border="false" title="汇总重量" :rightText="Number(totalWeight).toFixed(2)" />
				</uni-list>
			</view>
		</view>
		
		<!--  列表  -->
		<template v-if="purchaseList && purchaseList.length > 0">
			<uni-swipe-action>
				<uni-swipe-action-item :left-options="options" @click="handleDelete(item)" v-for="item in purchaseList">
					<view class="panel" @click="openDetail(item)">
						<view class="panel_title">
							<view class="panel_title_left">{{ item.materialName }}</view>
						</view>
						<view class="panel_content">
							<view class="panel_content_row">
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">批次：</view>
									<view class="panel_content_row_item_value">{{ item.batchNo }}</view>
								</view>
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">仓位：</view>
									<view class="panel_content_row_item_value">{{ item.locationNo }}</view>
								</view>
							</view>
							<view class="panel_content_row">
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">已入库：</view>
									<view class="panel_content_row_item_value">{{ Number(item.count).toFixed(2) }}{{ item.unit }}</view>
								</view>
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">剩余：</view>
									<view class="panel_content_row_item_value">
										{{ Number((item.colCount - item.count).toFixed(2)) }}{{ item.unit }}</view>
								</view>
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">总重：</view>
									<view class="panel_content_row_item_value">{{ Number(item.colCount).toFixed(2) }}{{ item.unit }}</view>
								</view>
							</view>
						</view>
						<view class="panel_dashboard">
							<view class="panel_dashboard_item">
								<view class="panel_dashboard_item_number success">{{ item.boxCount }}</view>
								<view class="panel_dashboard_item_value">已入库</view>
							</view>
							<view class="panel_dashboard_item">
								<view class="panel_dashboard_item_number error">{{ item.colBoxCount - item.boxCount }}
								</view>
								<view class="panel_dashboard_item_value">剩余</view>
							</view>
							<view class="panel_dashboard_item">
								<view class="panel_dashboard_item_number default">{{ item.colBoxCount }}</view>
								<view class="panel_dashboard_item_value">总数</view>
							</view>
						</view>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</template>
		<uni-empty-info v-else label="暂无物料信息"></uni-empty-info>

		<!--  底部按钮  -->
		<view class="footerPanel">
			<view class="footerPanel_body">
				<button @click="handleWarehousing" class="footerBtn save"><i
						class="iconfont icon-duihao btn_icon"></i>立即入库
				</button>
				<button @click="handleScanCode" class="footerBtn scan" type="primary"><i
						class="iconfont icon-saomiao btn_icon"></i>扫码
				</button>
			</view>
		</view>

		<!--  操作面板  -->
		<min-action-sheet ref="as"></min-action-sheet>

		<!-- 弹出层 -->
		<MaterialPopup ref="scaningPopup" title="物料入库" type="taskInStork" @success="initData"></MaterialPopup>
		<BarnPopup ref="barnPopup" @confirm="onLocationChange"></BarnPopup>
	</view>
</template>
<script>
	import topTab from "@/components/top-tab/top-tab"
	import minActionSheet from '@/components/min-action-sheet/min-action-sheet'
	import uniLoading from "../../vBase/components/uni-loading/uni-loading"
	import uniEmptyInfo from "../../vBase/components/uni-empty-info/uni-empty-info"
	import MaterialPopup from "@/components/popup/materialPopup"
	import BarnPopup from "@/components/popup/barnPopup"

	export default {
		components: {
			topTab,
			minActionSheet,
			uniLoading,
			uniEmptyInfo,
			MaterialPopup,
			BarnPopup
		},
		data() {
			return {
				loading: false,
				purchaseList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#FF4E74'
					}
				}],
				scaningShow: false,
				scanNo: null,
				isFouce: false,
				stockList: [],
				stockNo: '',
				locationNo: '',
				locationName: '',
				materialBomBillNo: '',
				taskBillNo: '',
				produceHouse: '',
				totalWeight: "0"
			}
		},
		async onLoad(e) {
			if (e.materialBomBillNo) this.materialBomBillNo = e.materialBomBillNo
			if (e.taskBillNo) this.taskBillNo = e.taskBillNo
			if (e.produceHouse) this.produceHouse = e.produceHouse
			await this.getStockList();
			this.initData()
		},
		onUnload() {
			uni.$off('deleteItem');
		},
		mounted() {
			let self = this
			uni.$on('deleteItem', ({
				material,
				success
			}) => {
				uni.showModal({
					title: '提示',
					content: '确认删除吗？',
					success(res) {
						if (res.confirm) {
							const {
								id,
							} = material;
							self.$api.deleteUserMaterialBomInMaterialBox({
								id
							}, {
								loading: true
							}).then((res) => {
								if (!res.res) return;
								self.initData();
								success && success();
							});
						}
					}
				})
			})
		},
		methods: {
			/** 查询仓库*/
			async getStockList() {
				this.loading = true;
				try {
					let {
						res,
						stockList = []
					} = await this.$api.getCustomerStock();
					if (!res) return;
					this.stockList = stockList;
					if (stockList.length) this.stockNo = stockList[0].stockNo;
				} finally {
					this.loading = false;
				}
			},
			/** 初始化数据 */
			initData() {
				if (!this.stockNo) return;
				this.pageConfig = {
					pageIndex: 0,
					pageSize: 20,
					bottomEnd: false,
					bottomLoading: true
				}
				this.getPurchaseList()
			},

			/**
			 *
			 */
			async openDetail(material) {
				const {
					id,
					stockNo,
					batchNo,
					materialName,
					materialNo
				} = material
				let {
					res,
					data = []
				} = await this.$api.getUserMaterialBomInMaterialBox({
					id: id
				}, {
					loading: true
				});
				let dataList = data.map(item => {
					return {
						id: item.id,
						materialName,
						materialNo,
						stockNo,
						batchNo,
						boxNo: item.boxNo,
						count: item.count,
						unit: item.unit,
					}
				})
				this.openPopNVue('入库明细', dataList)
			},

			/** 获取数据 */
			async getPurchaseList() {
				let {
					res,
					data = []
				} = await this.$api.getUserMaterialBomInMaterial({
					stockNo: this.stockNo
				});
				if (!res) return;
				this.purchaseList = data;
				this.loading = false;
				this.pageConfig.bottomEnd = true;
				this.pageConfig.bottomLoading = false;
				uni.stopPullDownRefresh();
				this.handleWeightTotal();
			},
			
			/**
			 * 计算总重量
			 */
			handleWeightTotal() {
				if (!this.purchaseList || !this.purchaseList.length) {
					this.totalWeight = "0"
					return
				}
				let num = 0
				this.purchaseList.forEach(v => {
					if (v.count) {
						num += v.count
					}
				})
				this.totalWeight = num.toString()
			},

			/** 选择仓位*/
			onLocationChange(location) {
				this.locationNo = location.locationNo;
				this.locationName = location.locationName;
			},

			/** 重置 */
			handleReset() {
				if (!this.stockNo) {
					uni.showToast({
						title: '请选择仓库',
						icon: 'none'
					});
					return;
				}
				if (!this.purchaseList || !this.purchaseList.length) {
					uni.showToast({
						title: '暂无物料',
						icon: 'none'
					});
					return;
				}
				const self = this
				uni.showModal({
					title: '提示',
					content: '是否重置？',
					success(res) {
						if (res.confirm) {
							self.$api.deleteUserPrdin({
								stockNo: self.stockNo,
							}, {
								loading: true
							}).then(res => {
								if (res.res) {
									uni.showToast({
										title: '物料重置成功',
										icon: 'none'
									})
									self.initData()
								}
							})
						}
					}
				})
			},

			/** 立即入库 */
			handleWarehousing() {
				const {
					stockNo,
					locationNo,
					purchaseList
				} = this;
				if (!stockNo) {
					uni.showToast({
						title: '请选择仓库',
						icon: 'none'
					});
					return;
				}
				if (!purchaseList || !purchaseList.length) {
					uni.showToast({
						title: '请扫码添加物料',
						icon: 'none'
					});
					return;
				}
				const self = this
				uni.showModal({
					title: '提示',
					content: '是否立即入库？',
					success(res) {
						if (res.confirm) {
							self.$api.createPrdInBill({
								stockNo: self.stockNo,
								taskBillNo: self.taskBillNo,
							}).then(res => {
								if (res.res) {
									uni.showToast({
										title: '入库成功',
										icon: 'none'
									})
									self.initData();
								}
							})
						}
					}
				})
			},

			/**
			 * 选择仓库
			 * @param e
			 */
			stockChange(e) {
				this.stockNo = this.stockList[e].stockNo
				this.initData()
			},

			/**
			 * 选择仓位
			 * @param e
			 */
			stockLocation(e) {
				this.locationNo = e.locationNo
				this.locationName = e.locationName
			},

			/**
			 * 选择
			 */
			handleBarnChoose() {
				this.$refs.barnPopup.open(this.stockNo);
			},

			/** 扫码 */
			async handleScanCode() {
				this.$refs.scaningPopup.open(this.stockNo, this.locationNo, this.materialBomBillNo, this.taskBillNo)
			},

			/**
			 * 删除物料
			 * @param {Object} e
			 */
			handleDelete(material) {
				const self = this
				uni.showModal({
					title: '提示',
					content: '是否删除物料？',
					success(res) {
						if (res.confirm) {
							self.$api.deleteUserMaterialBomInMaterial({
								id: material.id
							}, {
								loading: true
							}).then(res => {
								if (res.res) {
									uni.showToast({
										title: '物料删除成功',
										icon: 'none'
									})
									self.initData();
								}
							})
						}
					}
				})
			},

			/**
			 * 下拉刷新数据
			 */
			onPullDownRefresh() {
				this.initData()
			},

			/** 上滑加载 */
			onReachBottom() {
				if (!this.pageConfig.bottomEnd) {
					this.pageConfig.pageIndex++
					this.pageConfig.bottomLoading = true
					this.getPurchaseList();
				}
			},
		}
	}
</script>
<style lang="scss" scoped>
	@import "../../static/css/common";
</style>
